﻿
$$.DBCache = function () {
	var me = this;
	var cache = {};

	me.Set = function (tb, key, row, minute) {
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) return undefined;
		key = ("" + key).trim().toLowerCase();
		if (!key) return undefined;
		if (!row && row !== 0 && row !== false) return undefined;

		var tbo = cache[tb];
		if (!tbo) tbo = cache[tb] = {};

		if (!minute || !(minute > 0)) minute = 60;
		var now = new Date();
		now.setMinutes(now.getMinutes() + minute);
		tbo[key] = { ticks: now.getTime(), data: row };
		return true;
	};

	me.Sets = function (tb, keys, rows, minute) {
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) return undefined;
		if (!keys || $.type(keys) !== "array" || keys.length < 1) return undefined;
		if (!rows || $.type(rows) !== "array" || rows.length < 1) return undefined;

		var tbo = cache[tb];
		if (!tbo) tbo = cache[tb] = {};
		if (!minute || !(minute > 0)) minute = 60;
		var len = Math.min(keys.length, rows.length);
		for (var i = 0; i < len; i++) {
			var key = ("" + keys[i]).trim().toLowerCase();
			if (!key) continue;
			var row = rows[i];
			if (!row && row !== 0 && row !== false) continue;
			var now = new Date();
			now.setMinutes(now.getMinutes() + minute);
			tbo[key] = { ticks: now.getTime(), data: row };
		}
		return true;
	};

	me.Get = function (tb, key, callback) {
		// if (!callback) { console.error("db GetCache no callback:" + tb); return; }
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) { if (callback) callback(undefined); return undefined; }
		key = ("" + key).trim().toLowerCase();
		if (!key) { if (callback) callback(undefined); return undefined; }
		var tbo = cache[tb];
		if (!tbo) { if (callback) callback(null); return null; } 

		var row = tbo[key]; if (!row) { if (callback) callback(null); return null; }

		var ticks = row.ticks;
		if (!ticks || ticks < 10000) { delete tbo[key]; if (callback) callback(null); return null; }
		var now = new Date();
		if (now.getTime() > ticks) { delete tbo[key]; if (callback) callback(null); return null; }
		if (callback) callback(row.data);
		return row.data;
	};

	me.Gets = function (tb, keys, callback) {
		// if (!callback) { console.error("db GetCaches no callback:" + tb); return; }
		if (!keys || $.type(keys) !== "array" || keys.length < 1) { if (!callback) (undefined); return undefined; }
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) { if (callback) callback(undefined); return undefined; }
		var tbo = cache[tb];
		if (!tbo) { if (callback) callback(null); return null; }

		var rows = []; var uns = rows.uns = [];
		for (var i = 0; i < keys.length; i++) {
			rows[i] = null;
			var key = ("" + keys[i]).trim().toLowerCase();
			var row = tbo[key]; if (!row) { uns.push(i); continue; }

			var ticks = row.ticks;
			if (!ticks || ticks < 10000) { delete tbo[key]; uns.push(i); continue; }
			var now = new Date();
			if (now.getTime() > ticks) { delete tbo[key]; uns.push(i); continue; }
			rows[i] = row.data;
		}
		if (callback) callback(rows);
		return rows;
	}

	me.Remove = function (tb, key) {
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) return undefined;
		key = ("" + key).trim().toLowerCase();
		if (!key) return undefined;
		var tbo = cache[tb];
		if (!tbo) return false;
		delete tbo[key];
		return true;
	};

	me.Removes = function (tb, keys) {
		if (!keys || $.type(keys) !== "array" || keys.length < 1) return undefined;
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) return undefined;
		var tbo = cache[tb];
		if (!tbo) return false;

		for (var i = 0; i < keys.length; i++) {
			var key = ("" + keys[i]).trim().toLowerCase();
			if (key) delete tbo[key];
		}
		return true;
	};

	me.Clear = function (tb) {
		//tb=** 清除所有表
		tb = ("" + tb).trim().toLowerCase();
		if (!tb) return undefined;
		if (tb === "**") { cache = {}; return true; }
		var tbo = cache[tb];
		if (!tbo) return true;
		cache[tb] = {};
		return true;
	}
};